package com.irdstudio.efp.esb.service.impl.zx;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.constant.ESBTradeNoConstance;
import com.irdstudio.efp.esb.service.bo.req.BaseZXReqBean;
import com.irdstudio.efp.esb.service.bo.req.ShieldRiskDecisionQueryReports;
import com.irdstudio.efp.esb.service.bo.req.ShieldRiskHitQueryReports;
import com.irdstudio.efp.esb.service.bo.resp.zx.BaseZXResBean;
import com.irdstudio.efp.esb.service.bo.resp.zx.ShieldRiskDecisionQueryResponseReports;
import com.irdstudio.efp.esb.service.bo.resp.zx.ShieldRiskHitQueryResponseReports;
import com.irdstudio.efp.esb.service.vo.zx.ZXVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

@Service("zx11023Service")
@DependsOn("ZXConfig")
public class ZX11023ServiceImpl extends BaseZXService<ShieldRiskDecisionQueryReports,ShieldRiskDecisionQueryResponseReports>{
    private static Logger logger = LoggerFactory.getLogger(ZX11023ServiceImpl.class);

    @Override
    protected void preProcess(BaseZXReqBean<ShieldRiskDecisionQueryReports> reqBean) {
        ShieldRiskDecisionQueryReports shieldRiskDecisionQueryReports = reqBean.getQryRptsInfArry().get(0);
        shieldRiskDecisionQueryReports = new ShieldRiskDecisionQueryReports.
                Builder().
                buildWithExists(shieldRiskDecisionQueryReports).withIntfInd("").withMainTxnSrlNo("").
                build();
        logger.debug("进入同盾风险决策查询接口，参数：" + Objects.toString(shieldRiskDecisionQueryReports));
    }

    @Override
    protected ZXVO postProcess(BaseZXReqBean<ShieldRiskDecisionQueryReports> reqBean, BaseZXResBean<ShieldRiskDecisionQueryResponseReports> resBean) throws Exception {
        Optional.ofNullable(resBean.getRepRptsInfArry()).map(i->i.get((0))).orElseThrow(()->new ESBException("同盾风险决策查询返回为空！"));
        logger.debug("同盾风险决策规则查询进行后续处理，请求参数: " + reqBean + ", 返回结果： " + resBean);
        return new ZXVO(Boolean.TRUE, resBean.getRepRptsInfArry());
    }



    @Override
    protected String tradeNo() {
        return ESBTradeNoConstance.ZX11007;
    }

    @Override
    protected String scene() {
        return "02";
    }

    @Override
    protected Function<String, BaseZXResBean<ShieldRiskDecisionQueryResponseReports>> converter() {
        return i-> JSONObject.parseObject(i,new TypeReference<BaseZXResBean<ShieldRiskDecisionQueryResponseReports>>(){});
    }
}
